//package com.alibaba.cirtus.ots.application.adapter.ability.facade.service.dp.mndataif003;
//
//import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.CustomerAddressSDO;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.CustomerSDO;
//import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.CustomerSaleScopeSDO;
//import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
//import com.alibaba.cz.base.tool.log.Log;
//import com.epoch.app.bcots.model.dto.OrganizationSDO;
//import com.epoch.app.otsapplicationadaptersdk.dp.DpDelivery;
//import com.epoch.app.otsapplicationadaptersdk.dp.MnDataDeliveryResponse;
//import com.epoch.app.otsapplicationadaptersdk.dp.MnDataDeliverySDO;
//import com.epoch.app.otsapplicationadaptersdk.dp.mndataif003electricity.dto.DoHandlerRequest;
//import com.epoch.app.otsapplicationadaptersdk.dp.mndataif003electricity.service.MnDataIf003ElectricityService;
//import com.epoch.app.otsapplicationadaptersdk.model.dto.RestDpDeliveryDataElectricityRequest;
//import com.epoch.app.otsapplicationadaptersdk.service.DpService;
//import com.epoch.app.otsplatformenhance.domain.customer.model.Customer;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.compress.utils.Lists;
//import org.jetbrains.annotations.NotNull;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @author wangyibing
// * 送达方电商同步
// */
//@Component
//public class MnDataIf003ElectricityServiceImpl implements MnDataIf003ElectricityService {
//    Log log = Log.getLogger(MnDataIf003ElectricityServiceImpl.class);
//    @Resource
//    private MnDataIf003SDOBuilder mnDataIf003SDOBuilder;
//
//    @Resource
//    private MnDataIf003SyncUtils mnDataIf003SyncUtils;
//
//    @Resource
//    private DpService dpService;
//
//    @ProcessInvoker(remark = "MnDataIf003ElectricityService", errorCode = "", printLog = true)
//    @Override
//    public MnDataDeliveryResponse doHandler(DoHandlerRequest doHandlerRequest) {
//        // 1、构造中台查询参数
//        RestDpDeliveryDataElectricityRequest param = buildMnQueryMap(doHandlerRequest);
//        // 调用中台接口获取客户主数据
//        MnDataDeliveryResponse mnDataDeliveryResponse = getMnDataDeliveryResponse(param);
//        // 总页数
//        MnDataDeliverySDO data = mnDataDeliveryResponse.getData();
//        if (Objects.isNull(data) || CollectionUtils.isEmpty(data.getRows())) {
//            return mnDataDeliveryResponse;
//        }
//        int totalPage = getTotalPage(data);
//        log.info("电商送达方同步开始，总条数数据:{},共{}页", data.getTotalNum(), totalPage);
//        long before = System.currentTimeMillis();
//        // 2、分页同步客户主数据
//        for (int current = 1; current <= totalPage; current++) {
//            log.info("电商送达方同步开始，开始同步第{}页", current);
//            param.setPageNum(String.valueOf(current));
//            long start = System.currentTimeMillis();
//            MnDataDeliveryResponse mnRes = getMnDataDeliveryResponse(param);
//            if (Objects.isNull(mnRes.getData()) || CollectionUtils.isEmpty(mnRes.getData().getRows())) {
//                continue;
//            }
//            List<DpDelivery> dpDeliveries = mnRes.getData().getRows();
//            if (CollectionUtils.isEmpty(dpDeliveries)) {
//                continue;
//            }
//
//            try {
//                syncCurrentPageOfCustomerAddress(dpDeliveries);
//            } catch (Exception e){
//                log.error("MnDataIf003ElectricityServiceImpl#doHandler_fail " + "同步电商客户地址异常");
//                throw e;
//            }
//            log.info("电商送达方第{}页同步结束,用时{}", current, System.currentTimeMillis() - start);
//        }
//        log.info("电商送达方同步结束，耗时:{}", System.currentTimeMillis() - before);
//        return mnDataDeliveryResponse;
//    }
//
//    @NotNull
//    private MnDataDeliveryResponse getMnDataDeliveryResponse(RestDpDeliveryDataElectricityRequest param) {
//        MnDataDeliveryResponse mnDataDeliveryResponse = null;
//        int retryTimes = 0;
//        String errCode = null;
//        do {
//            if ("1108113002".equals(errCode)) {
//                try {
//                    Thread.sleep(120000);
//                } catch (InterruptedException e) {
//                    log.error("MnDataIf002ElectricityServiceImpl#doHandler InterruptedException", e);
//                }
//            }
//            mnDataDeliveryResponse = dpService.restDpDeliveryDataElectricity(param);
//            errCode = mnDataDeliveryResponse.getErrCode();
//        } while (!"0".equals(errCode) && retryTimes++ < 10);
//        return mnDataDeliveryResponse;
//    }
//
//    public int getTotalPage(MnDataDeliverySDO data) {
//        Integer totalNum = data.getTotalNum();
//        Integer pageSize = data.getPageSize();
//        int totalPage = 1;
//        if (totalNum % pageSize == 0) {
//            totalPage = totalNum / pageSize;
//        } else {
//            totalPage = (totalNum / pageSize) + 1;
//        }
//        return totalPage;
//    }
//
//    private void syncCurrentPageOfCustomerAddress(List<DpDelivery> mnCustomerAddress) {
//        Map<String, OrganizationSDO> organizationMapByCustomers = mnDataIf003SDOBuilder.getOrganizationMapByAddresses(mnCustomerAddress);
//        log.info("syncCurrentPageOfCustomerAddress,request,mnCustomerAddress:{}",mnCustomerAddress);
//        log.info("syncCurrentPageOfCustomerAddress,response,organizationMapByCustomers:{}",organizationMapByCustomers);
//        if (Objects.isNull(organizationMapByCustomers)) {
//            return;
//        }
//
//        List<CustomerSDO> customerSDOs = Lists.newArrayList();
//        List<CustomerSaleScopeSDO> customerSaleScopeSDOs = Lists.newArrayList();
//        List<CustomerAddressSDO> customerAddressSDOs = Lists.newArrayList();
//        for (DpDelivery address : mnCustomerAddress) {
//            // 客户组编码是0001，且客户编码和送达方编码一致，需要先生成客户、销售范围
//            if ("0001".equals(address.getKtokd()) && Objects.equals(address.getKunn2(), address.getKunnr())) {
//                CustomerSDO customerSDO = mnDataIf003SDOBuilder.buildCustomerMainData(address, organizationMapByCustomers);
//                if (Objects.isNull(customerSDO)) {
//                    continue;
//                }
//                customerSDOs.add(customerSDO);
//                CustomerSaleScopeSDO saleScopeSDO = mnDataIf003SDOBuilder.buildCustomerSaleScopeSDO(address, organizationMapByCustomers);
//                if (Objects.isNull(saleScopeSDO)) {
//                    continue;
//                }
//                customerSaleScopeSDOs.add(saleScopeSDO);
//            }
//            CustomerAddressSDO customerAddressSDO = mnDataIf003SDOBuilder.buildCustomerAddressSDO(address, organizationMapByCustomers);
//            if (Objects.isNull(customerAddressSDO)) {
//                continue;
//            }
//            customerAddressSDOs.add(customerAddressSDO);
//        }
//        // 批量新增或更新客户主档数据
//        batchSaveAndUpdateCustomers(customerSDOs);
//        // 批量新增或更新客户销售范围
//        batchSaveAndUpdateCustomerScopes(customerSaleScopeSDOs);
//        // 批量新增或更新客户收货地址
//        batchSaveAndUpdateAddresses(customerAddressSDOs);
//    }
//
//    private void batchSaveAndUpdateAddresses(List<CustomerAddressSDO> customerAddressSDOs) {
//        if (CollectionUtils.isEmpty(customerAddressSDOs)) {
//            return;
//        }
//        List<CustomerAddressSDO> existAddressList = mnDataIf003SyncUtils.batchQueryCustomerAddress(customerAddressSDOs);
//        Map<String, CustomerAddressSDO> existAddressMap = existAddressList.stream().collect(Collectors.toMap((it -> mnDataIf003SyncUtils.getAddressCombineKey(it)), a -> a, (a, b) -> a));
//        List<CustomerAddressSDO> notExistAddresses = customerAddressSDOs.stream().filter(it -> !existAddressMap.containsKey(mnDataIf003SyncUtils.getAddressCombineKey(it))).collect(Collectors.toList());
//        Map<String, CustomerAddressSDO> addressUpdatedMap = customerAddressSDOs.stream().filter(it -> existAddressMap.containsKey(mnDataIf003SyncUtils.getAddressCombineKey(it))).collect(Collectors.toMap(it -> mnDataIf003SyncUtils.getAddressCombineKey(it), a -> a, (a, b) -> a));
//        Boolean addressSave = mnDataIf003SyncUtils.batchCreateCustomerAddresses(notExistAddresses);
//        Boolean addressUpdated = mnDataIf003SyncUtils.batchUpdateCustomerAddresses(existAddressList, addressUpdatedMap);
//    }
//
//    private void batchSaveAndUpdateCustomerScopes(List<CustomerSaleScopeSDO> customerSaleScopeSDOs) {
//        if (CollectionUtils.isEmpty(customerSaleScopeSDOs)) {
//            return;
//        }
//        List<CustomerSaleScopeSDO> existScopeList = mnDataIf003SyncUtils.batchQueryCustomerScopes(customerSaleScopeSDOs);
//        Map<String, CustomerSaleScopeSDO> existScopeMap = existScopeList.stream().collect(Collectors.toMap((it -> mnDataIf003SyncUtils.getScopeCombineKey(it)), a -> a, (a, b) -> a));
//        List<CustomerSaleScopeSDO> notExistScopes = customerSaleScopeSDOs.stream().filter(it -> !existScopeMap.containsKey(mnDataIf003SyncUtils.getScopeCombineKey(it))).collect(Collectors.toList());
//        Map<String, CustomerSaleScopeSDO> updatedMap = customerSaleScopeSDOs.stream().filter(it -> existScopeMap.containsKey(mnDataIf003SyncUtils.getScopeCombineKey(it))).collect(Collectors.toMap(it -> mnDataIf003SyncUtils.getScopeCombineKey(it), a -> a, (a, b) -> a));
//        Boolean scopeSave = mnDataIf003SyncUtils.batchCreateCustomerScopes(notExistScopes);
//        Boolean scopeUpdate = mnDataIf003SyncUtils.batchUpdateCustomerScopes(existScopeList, updatedMap);
//    }
//
//    private void batchSaveAndUpdateCustomers(List<CustomerSDO> customerSDOs) {
//        if (CollectionUtils.isEmpty(customerSDOs)) {
//            return;
//        }
//        Set<String> customerCodeSet = customerSDOs.stream().map(CustomerSDO::getCode).collect(Collectors.toSet());
//        List<String> customerCodes = new ArrayList<>(customerCodeSet);
//        List<Customer> existCustomers = mnDataIf003SyncUtils.batchQueryCustomerMainData(customerCodes);
//        Map<String, Customer> existCustomerMap = existCustomers.stream().collect(Collectors.toMap(Customer::getCode, a -> a, (a, b) -> a));
//        Map<String, CustomerSDO> updateMap = customerSDOs.stream().filter(it -> existCustomerMap.containsKey(it.getCode())).collect(Collectors.toMap(CustomerSDO::getCode, a -> a, (a, b) -> a));
//        List<CustomerSDO> notExistCustomers = customerSDOs.stream().filter(it -> !existCustomerMap.containsKey(it.getCode())).collect(Collectors.toList());
//        //处理新增的客户
//        Boolean save = mnDataIf003SyncUtils.batchCreateCustomers(notExistCustomers);
//        Boolean update = mnDataIf003SyncUtils.batchUpdateCustomers(existCustomers, updateMap);
//    }
//
//    private RestDpDeliveryDataElectricityRequest buildMnQueryMap(DoHandlerRequest doHandlerRequest) {
//        RestDpDeliveryDataElectricityRequest dataGroupRequest = RestDpDeliveryDataElectricityRequest.builder()
//                .fromSys(doHandlerRequest.getFromSys())
//                .pageNum(doHandlerRequest.getPageNum())
//                .pageSize(doHandlerRequest.getPageSize())
//                .ds(doHandlerRequest.getDs())
//                .build();
//        return dataGroupRequest;
//    }
//}
